﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Generator.Tools
{
    public partial class ClearLicx : Form
    {

        delegate void processFile(string fileName);


        public ClearLicx()
        {
            InitializeComponent();
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            DirectoryInfo di = new DirectoryInfo(textBox1.Text);

            processFile pro = new processFile(ClearLicxFile);
            DoProcessLicxFile(di, pro);
        }


        private void ClearLicxFile(string fileName)
        {

            var fi = new System.IO.FileInfo(fileName);
            richTextBox1.AppendText(string.Format("{0}:{1} -> 0", fi.FullName, fi.Length));
            richTextBox1.AppendText(Environment.NewLine);

            if (fi.IsReadOnly)
            {
                fi.Attributes = FileAttributes.Normal;
            }
            //richTextBox1.AppendText(
            using (var fs = fi.OpenWrite())
            {
                fs.Position = 0;
                fs.SetLength(0);
                fs.Close();
            }


        }

        private void BackupLicxFile(string fileName)
        {
            var fi = new System.IO.FileInfo(fileName);

            richTextBox1.AppendText(string.Format("{0}:{1}", fi.FullName, fi.Length));
            richTextBox1.AppendText(Environment.NewLine);


            if (fi.Name.ToLower().CompareTo("licenses.licx") == 0)
            {
                System.IO.File.Copy(fi.FullName, fi.FullName + ".bak", true);
            }
        }

        private void RestoreLicxFile(string fileName)
        {
            var fi = new System.IO.FileInfo(fileName);
            string sourceFile = fi.FullName + ".bak";
            if (fi.Name.ToLower().CompareTo("licenses.licx") == 0 && System.IO.File.Exists(sourceFile))
            {
                System.IO.File.Copy(sourceFile, fi.FullName, true);
            }

            fi = new System.IO.FileInfo(fileName);
            richTextBox1.AppendText(string.Format("{0}:{1}", fi.FullName, fi.Length));
            richTextBox1.AppendText(Environment.NewLine);

        }

        private void ShowLicxFile(string fileName)
        {
            var fi = new System.IO.FileInfo(fileName);
            richTextBox1.AppendText(string.Format("{0}:{1}", fi.FullName, fi.Length));
            richTextBox1.AppendText(Environment.NewLine);
        }



        private void DoProcessLicxFile(DirectoryInfo direcory, processFile pro)
        {

            foreach (FileInfo fi in direcory.GetFiles("licenses.licx"))
            {

                pro(fi.FullName);
            }

            foreach (DirectoryInfo di in direcory.GetDirectories())
            {
                DoProcessLicxFile(di, pro);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = textBox1.Text;
            var res = folderBrowserDialog1.ShowDialog();
            if (res != DialogResult.OK)
                return;

            textBox1.Text = folderBrowserDialog1.SelectedPath;

            showfiles();

        }

        private void showfiles()
        {

            richTextBox1.Clear();
            DirectoryInfo di = new DirectoryInfo(textBox1.Text);

            processFile pro = new processFile(ShowLicxFile);
            DoProcessLicxFile(di, pro);

        }

        private void backupLicxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            DirectoryInfo di = new DirectoryInfo(textBox1.Text);

            processFile pro = new processFile(BackupLicxFile);
            DoProcessLicxFile(di, pro);
        }



        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();

            var hWnd = WindowsHookHelper.GetForegroundWindow();
            //var hd = Convert.ToString(hWnd.ToInt32(), 16).ToLower();
            //if ("000b1802".EndsWith(hd))
            //{ 

            //}

            StringBuilder lpClassName = new StringBuilder(256);
            //string lp = new string(new char[255]);
            WindowsHookHelper.GetWindowText(hWnd, lpClassName, 255);



            if (lpClassName.ToString().EndsWith("のバージョン情報") || lpClassName.ToString().StartsWith("ライセンスについて"))
            {
                SendKeys.SendWait("{ENTER}");
            }
            timer1.Start();
        }

        private void autoCloseC1DialogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            autoCloseC1DialogToolStripMenuItem.Checked = !autoCloseC1DialogToolStripMenuItem.Checked;
            if (autoCloseC1DialogToolStripMenuItem.Checked)
            {
                timer1.Start();
            }
            else
            {
                timer1.Stop();
            }
        }

        private void ClearLicx_Load(object sender, EventArgs e)
        {
            if (autoCloseC1DialogToolStripMenuItem.Checked)
            {
                timer1.Start();
            }
        }

        private void ClearLicx_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
        }

        private void restoreLicxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            DirectoryInfo di = new DirectoryInfo(textBox1.Text);

            processFile pro = new processFile(RestoreLicxFile);
            DoProcessLicxFile(di, pro);

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                showfiles();
            }
        }

        private void replacecsprojToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var f1 = string.Format("{0}\\H00Karte\\Starter\\H00Karte.Starter.csproj", textBox1.Text);
            var f2 = string.Format("{0}\\H00Karte\\BoostPack\\BoostPack.csproj", textBox1.Text);

            System.IO.File.Copy(f1 + ".bak", f1, true);
            System.IO.File.Copy(f2 + ".bak", f2, true);

        }



    }
}
